package com.guisu.material.behavior

import android.animation.Animator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat

class TranslationUpBehavior(context: Context?, attrs: AttributeSet?) :
    CoordinatorLayout.Behavior<View>(context, attrs) {
    // 关注垂直滚动，而且向上的时候是出来，向下是隐藏
    override fun onStartNestedScroll(
        coordinatorLayout: CoordinatorLayout,
        child: View,
        directTargetChild: View,
        target: View,
        axes: Int,
        type: Int
    ): Boolean {
        // 如果onStartNestedScroll返回true，后面的nestedScroll方法才会执行
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL
    }

    private var isOut = false
    private var animating = false
    override fun onNestedScroll(
        coordinatorLayout: CoordinatorLayout,
        child: View,
        target: View,
        dxConsumed: Int,
        dyConsumed: Int,
        dxUnconsumed: Int,
        dyUnconsumed: Int,
        type: Int,
        consumed: IntArray
    ) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed)
        Log.e("TAG", "dyConsumed -> $dyConsumed, dyUnconsumed -> $dyUnconsumed")
        if (dyConsumed > 0) {
            if (!isOut && !animating) {
                isOut = true
                animating = true
                // 往上滑动，是出来，加一个标志位  已经往下走了
                val translationY = (child.layoutParams as CoordinatorLayout.LayoutParams).topMargin + child.measuredHeight
                val animator = child.animate().translationY(-translationY.toFloat()).setDuration(500L)
                animator.setListener(object : Animator.AnimatorListener {
                    override fun onAnimationStart(animation: Animator) {}
                    override fun onAnimationEnd(animation: Animator) {
                        animation.removeAllListeners()
                        animating = false
                    }
                    override fun onAnimationCancel(animation: Animator) {}
                    override fun onAnimationRepeat(animation: Animator) {}
                })
                animator.start()
            }
        } else if (dyConsumed < 0) {
            if (isOut && !animating) {
                isOut = false
                animating = true
                // 往下滑动
                val animator = child.animate().translationY(0f).setDuration(500L)
                animator.setListener(object : Animator.AnimatorListener {
                    override fun onAnimationStart(animation: Animator) {}
                    override fun onAnimationEnd(animation: Animator) {
                        animation.removeAllListeners()
                        animating = false
                    }
                    override fun onAnimationCancel(animation: Animator) {}
                    override fun onAnimationRepeat(animation: Animator) {}
                })
                animator.start()
            }
        }
    }
}